SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO




ALTER  trigger tb_comexpproportionfinished_au on tb_comexpproportionfinished for update 
as
begin
  set nocount on
  declare @nbid int, 
          @nbno int,
          @nrelationcount int, 
          @nrelationbtype int,
          @nrelationbid int,
	  @nrelationbno int,
          @namount decimal(18,2),
          @obid int,
          @obno int, 
          @orelationcount int,
          @orelaionbtype int,
          @orelationbid int,
	  @orelationbno int,
          @oamount decimal(18,2)
  declare tb_comexpproportionfinished_cur cursor local forward_only static optimistic for 
  select n.bid,n.bno,n.relationcount,n.relationbtype,n.relationbid,n.relationbno,n.amount,
  o.bid, o.bno, o.relationcount,o.relationbtype,o.relationbid,o.relationbno,o.amount
  from inserted n, deleted o where n.bid = o.bid and n.bno = o.bno
  open tb_comexpproportionfinished_cur

  fetch next from tb_comexpproportionfinished_cur into 
  @nbid, @nbno, @nrelationcount, @nrelationbtype, @nrelationbid, @nrelationbno, @namount,
  @obid, @obno, @orelationcount, @orelaionbtype, @orelationbid, @orelationbno,@oamount
  while (@@fetch_status = 0)
  begin
    if (@namount<>@oamount or @nrelationbid<>@orelationbid)
    begin
      update tb_comcheckacceptdtl set relationcount=relationcount-1,  expense = expense - @oamount  
        where bid=@orelationbid and bno=@orelationbno

      update tb_comcheckacceptdtl set relationcount=relationcount+1,  expense = expense + @namount  
       where bid=@nrelationbid and bno=@nrelationbno
    end
    fetch next from tb_comexpproportionfinished_cur into 
    @nbid, @nbno, @nrelationcount, @nrelationbtype, @nrelationbid,@nrelationbno,@namount,
    @obid, @obno, @orelationcount, @orelaionbtype, @orelationbid, @orelationbno,@oamount
  end
  close tb_comexpproportionfinished_cur
  deallocate tb_comexpproportionfinished_cur
end




GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

